package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.controller.admin.DishContorller;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();//得到该用户id
        log.info("需要新增操作的购物车用户是：{}",currentId);

        log.info("要操作的数据{}",shoppingCartDTO);
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(currentId);

        log.info("判断数据是否存在在购物车，存在就使得count变化");
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //如果已经存在，就更新数量，数量加1
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            log.info("判断要插入的数据是套餐还是菜品");
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId != null) {
                log.info("是菜品");
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }else if (setmealId != null) {
                log.info("是菜品");
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            shoppingCartMapper.insert(shoppingCart);
            log.info("操作完成");
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {

        return shoppingCartMapper.list(ShoppingCart.builder().userId(BaseContext.getCurrentId()).build());
    }

    /**
     * 清除购物车
     * @param currentId
     */
    @Override
    public void clean(Long currentId) {
        log.info("清除user{}的购物车",currentId);
        shoppingCartMapper.cleanAll(currentId);
    }

    /**
     * 减少或删除菜品
     * @param shoppingCartDTO
     */
    @Override
    public void reduce(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        log.info("要操作的用户是{}",currentId);
        log.info("操作对象内容是{}",shoppingCartDTO);
        log.info("查看是否有多个口味信息");
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setDishFlavor(null);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list.size()>1) {
            log.info("口味不唯一");
            for (ShoppingCart cart : list) {
                if (cart.getDishFlavor().equals(shoppingCartDTO.getDishFlavor())){
                    subNumber(cart);
                }
            }
        }else {
            log.info("口味唯一");
            ShoppingCart cart = list.get(0);
            subNumber(cart);
        }
    }

    private void subNumber(ShoppingCart cart) {
        cart.setNumber(cart.getNumber()-1);
        if (cart.getNumber() != 0) {
            shoppingCartMapper.updateNumber(cart);
            return;
        }
        shoppingCartMapper.deleteShop(cart);
    }
}
